# Maintainer: Daniel M. Capella <polyzen@archlinux.org>
# Contributor: hexchain <i at hexchain dot org>

pkgname=yaml-language-server
pkgver=1.12.0
pkgrel=1
pkgdesc='YAML Language Server'
url=https://github.com/redhat-developer/yaml-language-server
license=('MIT')
arch=('any')
depends=('nodejs')
makedepends=('jq' 'yarn')
options=('!emptydirs')
source=("$url/archive/$pkgver/$pkgname-$pkgver.tar.gz"
        'yamlls-pr-863.patch::https://github.com/redhat-developer/yaml-language-server/pull/863.patch')
b2sums=('40f6dd646c88589a48a132557dcfe5b18bd9c70724fc3cfd6acdfc4cc61174cb2165c90435d48f4ac70844c8cdb33390de550b2a95a5d1f330e4d1283d673b7a'
        '81ce25ab8b09356e1506bb6e1dea18f8dd53007ea9bf1653de45b4d4a8acde140359cc37afa5a88ae9a7bf080cc40b6f0733661457498ebdbf618001d704b4f9')

prepare() {
  cd $pkgname-$pkgver
  patch --forward --strip=1 --input=../yamlls-pr-863.patch
}

build() {
  cd $pkgname-$pkgver
  yarn --frozen-lockfile
  yarn compile
}

check() {
  cd $pkgname-$pkgver
  yarn test
}

package() {
  cd $pkgname-$pkgver

  # Emulate `npm prune --production`
  cp package.json{,.bak}
  read -ra devDependencies < <(jq -r '.devDependencies | keys | join(" ")' package.json)
  yarn remove --frozen-lockfile "${devDependencies[@]}"
  mv package.json{.bak,}

  install -d "$pkgdir"/usr/{bin,lib/node_modules/$pkgname}
  ln -s ../lib/node_modules/$pkgname/bin/$pkgname "$pkgdir"/usr/bin/$pkgname
  cp -r bin node_modules out package.json \
    "$pkgdir"/usr/lib/node_modules/$pkgname
  install -Dm644 -t "$pkgdir"/usr/share/doc/$pkgname {CHANGELOG,README}.md
  install -Dm644 -t "$pkgdir"/usr/share/licenses/$pkgname LICENSE
}
